const http = require("http")

const data = require("./data/index.json")

const severs = http.createServer((req, res) => {
    let url = new URL(req.url,'http://127.0.0.1')
   console.log(url);
   res.setHeader("content-type","text/html;charset=utf-8")
   if(url.pathname === "/"){
    let str = `<ul>`
    data.forEach((item) => {
        str += `<li><a href="/detail?name=${item.id}">${item.newsTitle}</a></li>`
    })
    str += `</ul>`
    res.end(str)
   }

   if(url.pathname === "/detail"){
        let str1 = url.searchParams.get("name")
        let obj = data.find((item)=>{
            return item.id === str1
        })

        let str2 = `
        <h3>${obj.newsTitle} </h3>
        <p>${obj.newsContent}</p>
        `
        res.end(str2 )
   }
})





severs.listen(80, () => {
    console.log("监听成功");
})